package com.rfid.project.web;

import java.text.DateFormat;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.data.domain.Page;

import biz.util.IdUtil;
import biz.appframe.PageJsonBean;

import com.rfid.project.exception.CannotDelException;
import com.rfid.project.model.Customer;
import com.rfid.project.service.CustomerService;

@Controller
public class CustomerController {

	private static final Logger logger = LoggerFactory
			.getLogger(CustomerController.class);

	@Autowired
	private CustomerService customerService;

	@RequestMapping(value = "/customermgr")
	public String customermgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return "customermgr";
	}
	
	@RequestMapping(value = "/customerQuery")
	public String customerQuery(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return "customerQuery";
	}

	@RequestMapping(value = "/queryCustomer")
	public String queryCustomer(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "code", defaultValue = "") String code,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "address", defaultValue = "") String address,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "20") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<Customer> customers = customerService.findAll(code, name, address,
				remark, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", customers.getTotalElements());
		JSONArray models = new JSONArray();
		for (Customer customer : customers) {
			JSONObject mod = new JSONObject();
			wrapData(customer, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/findCustomer")
	public String findCustomer(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			Model model) {
		Customer customer = customerService.findOne(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(customer, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}

	private void wrapData(Customer customer, JSONObject mod) {
		mod.put("id", customer.getId());
		mod.put("name", customer.getName());
		mod.put("code", customer.getCode());
		mod.put("address", customer.getAddress());
		String content = customer.getRemark();
		mod.put("remark", content == null ? "" : content);
	}

	@RequestMapping(value = "/editCustomer", method = RequestMethod.POST)
	public String editCustomer(HttpServletRequest request,
			HttpServletResponse response, @Valid Customer customer,
			Model model, RedirectAttributes redirectAttributes) {
		customerService.saveCustomer(customer);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delCustomer")
	public String delCustomer(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		try {
			this.customerService.deleteCustomer(id);
		} catch (CannotDelException e) {
			result.put("msg", e.getMessage());
		}
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat();
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
